package com.iscas.green.middewaretest.controller;


import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.sql.Connection;

// MessageController.java
@RestController
@RequestMapping("/api/messages")
@Slf4j
public class MessageController {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private LettuceConnectionFactory lettuceConnectionFactory;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @PostMapping("/kafka")
    public ResponseEntity<String> sendKafkaMessage(@RequestBody String message) {
        try {
            kafkaTemplate.send("chaos-topic", message);
            log.info("Sent message to Kafka: {}", message);
            return ResponseEntity.ok("Message sent to Kafka");
        } catch (Exception e) {
            log.error("Failed to send Kafka message", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }

    @PostMapping("/rabbitmq")
    public ResponseEntity<String> sendRabbitMQMessage(@RequestBody String message) {
        try {
            rabbitTemplate.convertAndSend("chaos-exchange", "chaos-routing-key", message);
            log.info("Sent message to RabbitMQ: {}", message);
            return ResponseEntity.ok("Message sent to RabbitMQ");
        } catch (Exception e) {
            log.error("Failed to send RabbitMQ message", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }

    @PostMapping("/rocketmq")
    public ResponseEntity<String> sendRocketMQMessage(@RequestBody String message) {
        try {
            rocketMQTemplate.convertAndSend("chaos-topic", message);
            log.info("Sent message to RocketMQ: {}", message);
            return ResponseEntity.ok("Message sent to RocketMQ");
        } catch (Exception e) {
            log.error("Failed to send RocketMQ message", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }
    @PostMapping("/jedis")
    public ResponseEntity<String> testJedis(@RequestBody String value) {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.set("jedis-key", value);
            log.info("Set value using Jedis: {}", jedis.get("jedis-key"));
            return ResponseEntity.ok("Successfully tested Jedis client");
        } catch (Exception e) {
            log.error("Jedis operation failed", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("Jedis error: " + e.getMessage());
        }
    }
    @PostMapping("/redisson")
    public ResponseEntity<String> testRedisson(@RequestBody String value) {
        try {
            RMap<String, String> map = redissonClient.getMap("redisson-map");
            map.put("redisson-key", value);
            log.info("Set value using Redisson: {}", map.get("redisson-key"));
            return ResponseEntity.ok("Successfully tested Redisson client");
        } catch (Exception e) {
            log.error("Redisson operation failed", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("Redisson error: " + e.getMessage());
        }
    }
    @PostMapping("/lettuce")
    public ResponseEntity<String> testLettuce(@RequestBody String value) {
        try (RedisConnection connection = lettuceConnectionFactory.getConnection()) {
            connection.set("lettuce-key".getBytes(), value.getBytes());
            log.info("Set value using Lettuce: {}",
                    new String(connection.get("lettuce-key".getBytes())));
            return ResponseEntity.ok("Successfully tested Lettuce client");
        } catch (Exception e) {
            log.error("Lettuce operation failed", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("Lettuce error: " + e.getMessage());
        }
    }

    @PostMapping("/postgresql")
    public ResponseEntity<String> testPostgreSQL(@RequestBody String data) {
        try {
            jdbcTemplate.update("CREATE TABLE IF NOT EXISTS chaos_test (id SERIAL PRIMARY KEY, data TEXT)");
            jdbcTemplate.update("INSERT INTO chaos_test (data) VALUES (?)", data);

            return ResponseEntity.ok("Successfully inserted data into PostgreSQL");
        } catch (Exception e) {
            log.error("PostgreSQL operation failed", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }
}